//
//  HouseTypeViewController.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/5/12.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "HouseTypeViewController.h"
#import "HouseDetailCell1.h"
#import "HouseDetailCell2.h"
#import "NearbyCell2.h"
#import "HouseDetailDataManage.h"
#import "HouseTypeData.h"
#import "UserModal.h"
#import "SingletonData.h"
#import "NodataCell.h"
#import "DistanceCell.h"
#import "FangYuanCell.h"
#import "FangXingCell.h"
#import "HouseIntroducedCell.h"
#import "disDescModel.h"
#import "NearbyCellData.h"
#import "TxtReadVc.h"
#import "MapVc.h"
#import "HourseMapCellData.h"

#import "config.h"
#import "LineCell.h"
#import "MapVC2.h"
#import "CustomAnnotation.h"
#import "MapViewController.h"



@interface HouseTypeViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
@property(nonatomic,strong)UITableView *tableView1;
@property(nonatomic,strong)UITableView *tableView2;
@property (strong, nonatomic) HouseDetailDataManage *dataManage;
@property (strong, nonatomic) HouseTypeData *model;
@property (strong, nonatomic) NodataCell *nodataCell;

@property (nonatomic,strong)UITableViewCell *fxcel,*fZcell;

@property(nonatomic,assign) NSInteger flag;//flag = 0 tableview1,falg = 2 tableview2;
@end

@implementation HouseTypeViewController
@synthesize nodataCell;

- (void)viewDidLoad {
    
    [super viewDidLoad];

    [self initTableView];
    
    [self freshData];
    
}


-(void)initTableView
{
    
    [self.titleView setMianTitle:@"房屋详情"];
    self.titleView.doneBtn.hidden = YES;
    self.flag = 1;
    self.dataManage = [[HouseDetailDataManage alloc]init];
    
    UITableView *tableView1 = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UIScreenWidth, UIScreenHeight - 64)];
    tableView1.delegate = self;
    tableView1.dataSource = self;
    tableView1.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:tableView1];
    self.tableView1 = tableView1;
    [self.tableView1 setBackgroundColor:UIColorFromRGB(0xf3f3f3)];
   
    
    
//    UITableView *tableView2 = [[UITableView alloc]initWithFrame:CGRectMake(0, UIScreenHeight - 64, UIScreenWidth, UIScreenHeight - 64)];
//    tableView2.delegate = self;
//    tableView2.separatorStyle = UITableViewCellSelectionStyleNone;
//    tableView2.dataSource = self;
//    tableView2.allowsSelection = NO;
//    [self.view addSubview:tableView2];
//    self.tableView2 = tableView2;
//     [self.tableView2 setBackgroundColor:UIColorFromRGB(0xf3f3f3)];

    
}
#pragma mark 把距离表述，筛选到最多只有三条（中国城，学区房，商业中心）
-(void)selectDisdesc
{
    NSInteger i = 0,j = 0,k = 0;
    NSMutableArray *arrM = [NSMutableArray arrayWithArray:self.model.disDescArr];
    if (arrM.count == 0)
    {
        self.model.disDescArr = nil;
    }
    else
    {
        for (disDescModel *mod in arrM)
        {
            
            if ([mod.lTypeName isEqual:@"中国城房"])
            {
                i ++;
                if (i > 1)
                {
                    [self.model.disDescArr removeObject:mod];
                }
            }else if([mod.lTypeName isEqual:@"学区房"])
            {
                j ++;
                if (j > 1)
                {
                    [self.model.disDescArr removeObject:mod];
                }
                
            }else
            {
                k ++;
                if (k > 1)
                {
                    [self.model.disDescArr removeObject:mod];
                }
                
            }
            
        }
        
    }
    
}


#pragma mark 获取数据
-(void)freshData
{
    
    [nodataCell setState:nodataCellStateLoading];
    
    __weak typeof(self) bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        
        
        ResulteMod *mod = [self.dataManage refreshDataWithConsumerId:[NSString stringWithFormat:@"%ld",(long)[UserModal shareUserModel].userId] WithPremiseshousingId:self.premiseshousingId];
       
        dispatch_async(dispatch_get_main_queue(), ^{
        
            if (mod.resltState == ResultNetError) {//网络出错
                
                [bself.nodataCell setState:nodataCellStateNetError];
                
            }else if(mod.resltState == ResultFail){//服务器返回的code出错
                
                [bself.nodataCell setCodeErrorTip:mod.resultCode];
                
            }else{//返回正常
                
                 self.model = mod.dataMod;
                
                if(self.model == nil){//没数据
                    
                    [bself.nodataCell setState:nodataCellStateNodata];
                }
            }
            
            [bself.tableView1 reloadData];
            
            
        });
    });
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    NSInteger num = 0;
//    if (self.model!=nil)
//    {
//        if (self.flag == 1)
//        {
//            num =  self.model.nearbyArr.count + 3 ;
//        }else if (self.flag == 2)
//        {
//            num =  4;
//        }
//        
//    }else{
//        num =  1;
//    }
//    return num;
    
    NSInteger num = 0;
    if (self.model!=nil)
    {
        num =  self.model.nearbyArr.count + 7 ;
    }else{
        num =  1;
    }
    return num;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.model == nil)
    {
        if(!nodataCell){
            nodataCell = [[NodataCell alloc] initWithFrame:self.tableView1.frame];
            [nodataCell setState:nodataCellStateLoading];
            [[nodataCell refreshBtn]setTag:56];
            [nodataCell.refreshBtn addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        }
        return nodataCell;
    }
    else
    {
            if (indexPath.row == 0)
            {
                HouseDetailCell1 *cell = [HouseDetailCell1 cellWithTableView:tableView data:self.model];
                cell.bufNvc = self.navigationController;
                cell.data1 = self.model;
                return cell;
            }else if (indexPath.row == self.model.nearbyArr.count + 1)
            {
                return [LineCell cellWithTableView:tableView];
            }
            else if (indexPath.row == self.model.nearbyArr.count + 2)
            {
                HouseDetailCell2 *cell = [HouseDetailCell2 cellWithTableView:tableView];
                cell.data = self.model;
                return cell;
            }else if (indexPath.row == self.model.nearbyArr.count + 3)
           {
                return [HouseIntroducedCell cellWithTableView:tableView WithDetail:self.model.detail WithDetailEnglish:self.model.detailEnglish];
           }else if(indexPath.row == self.model.nearbyArr.count + 4)
           {
              [self selectDisdesc];
        
               return [DistanceCell cellWithTable:tableView withDisDescArr:self.model.disDescArr];
        
          }else if (indexPath.row == self.model.nearbyArr.count + 5)
         {
        
            FangXingCell *cell =  [FangXingCell cellWithTableView:tableView WithFangXingPicsArr:self.model.fangxingPicsArr];
             cell.bufNvc = self.navigationController;
             return cell;
         }
        else if(indexPath.row == self.model.nearbyArr.count + 6)
       {
        FangYuanCell *cell =  [FangYuanCell cellWithTableView:tableView WithFangXingPicsArr:self.model.fangyuanPicsArr];
           cell.bufNvc = self.navigationController;
           return cell;
        }else
            {
                NearbyCell2 *cell = [NearbyCell2 cellWithTableView:tableView withData:[self.model.nearbyArr objectAtIndex:indexPath.row - 1]];
                cell.data = [self.model.nearbyArr objectAtIndex:indexPath.row - 1];
                return cell;
            }
    
    }

    return nil;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (self.model!=nil) {
//        
//        [tableView setScrollEnabled:YES];
//        if (self.flag ==1)
//        {
//            if (indexPath.row == 0)
//            {
//                return 609;
//            }else if (indexPath.row == self.model.nearbyArr.count +1)
//            {
//                return [LineCell getHeightWithNearbyArr:self.model.nearbyArr];
//            }
//            else if (indexPath.row == self.model.nearbyArr.count +2)
//            {
//
//                return 254;
//            }else
//            {
//                if (self.model.nearbyArr.count == 0) {
//                    return 0;
//                }
//                return 55;
//            }
//
//        }else if (self.flag ==2)
//        {
//            if (indexPath.row == 1)
//            {
//                return [DistanceCell getHeightWithTable:self.tableView2 withDisDescArr:self.model.disDescArr];
//                
//            }
//            else if(indexPath.row == 2)
//            {
//    
//                return [FangXingCell getHeightWithTableView:self.tableView2 WithFangXingPicsArr:self.model.fangxingPicsArr];
//            }
//            else if(indexPath.row == 3)
//            {
//                
//            return [FangYuanCell getHeightWithTableView:self.tableView2 WithFangYuanPicsArr:self.model.fangyuanPicsArr];
//                
//            }
//            else
//            {
//                
//                return [HouseIntroducedCell getHeightWithWithDetail:self.model.detail WithDetailEnglish:self.model.detailEnglish WithTableView:self.tableView2];
//            }
//
//        }
//        return 0;
//        
//      }else{
//          [tableView setScrollEnabled:NO];
//        return tableView.frame.size.height;
//    }
    
    if (self.model!=nil) {
        
        [tableView setScrollEnabled:YES];
        
            if (indexPath.row == 0)
            {
                return [HouseDetailCell1 getHeighWithTableView:tableView data:self.model];
            }else if (indexPath.row == self.model.nearbyArr.count +1)
            {
                return [LineCell getHeightWithNearbyArr:self.model.nearbyArr];
            }
            else if (indexPath.row == self.model.nearbyArr.count +2)
            {
                
                return 220;
            }else
        if (indexPath.row == self.model.nearbyArr.count+4)
        {
            return [DistanceCell getHeightWithTable:tableView withDisDescArr:self.model.disDescArr];
            
        }
        else if(indexPath.row == self.model.nearbyArr.count+5)
        {
            
            return [FangXingCell getHeightWithTableView:tableView WithFangXingPicsArr:self.model.fangxingPicsArr];
        }
        else if(indexPath.row == self.model.nearbyArr.count+6)
        {
            
            return [FangYuanCell getHeightWithTableView:tableView WithFangYuanPicsArr:self.model.fangyuanPicsArr];
            
        }
        else if(indexPath.row == self.model.nearbyArr.count+3)
        {
            
            return [HouseIntroducedCell getHeightWithWithDetail:self.model.detail WithDetailEnglish:self.model.detailEnglish WithTableView:tableView];
        }
        else
            {
                return [NearbyCell2 getHight:[self.model.nearbyArr objectAtIndex:indexPath.row - 1] withTableView:tableView];
            }
        
    }else{
        [tableView setScrollEnabled:NO];
        return tableView.frame.size.height;
    }

    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.model != nil)
    {

        if (indexPath.row == self.model.nearbyArr.count + 2)
        {

             CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(self.model.latitude,self.model.longitude);
            CustomAnnotation *mapData = [[CustomAnnotation alloc]initWithCoordinate:coords];
            
            MapViewController *vc = [[MapViewController alloc]initWithLoc:mapData];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
   
}
@end
